//
//  RootController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/18.
//  Copyright © 2015年 game. All rights reserved.

#import "RootController.h"
#import "AppDelegate.h"
#import "Manager.h"
#import "AticHeadView.h"
#import "ChangyanSDK.h"
#import "UserDataModel.h"
#import "UIImageView+WebCache.h"
#import "RootTableview.h"

@interface RootController ()<MBProgressHUDDelegate>

@property (nonatomic,assign) BOOL DataGet;
@end

@implementation RootController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    self.Myud = [NSUserDefaults standardUserDefaults];
    
    self.view.backgroundColor  = [UIColor whiteColor];
    
    /**
     *  如果用户登录成功 就把畅言也登录上
     */
    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        self.UserName =[ud valueForKey:@"userName"];
        self.UserPass = [ud valueForKey:@"userPass"];
        
//        //账户密码，加密字典
//        self.UserDic = @{
//                        @"username":[Manager doEncryptStr:self.UserName withKey:@"diaoyurbbs"],
//                        @"password":[Manager doEncryptStr:self.UserPass withKey:@"diaoyurbbs"],
//                        };
        
    }
}


/**
 *
 *建立文章详情视频详情的导航栏
 */

-(UIView *)CreatNavViewWhithTitle:(NSString *)title{
    
    _Navview = [[AticHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    _Navview.backgroundColor = [UIColor whiteColor];
    _Navview.UserName = self.UserName;
    _Navview.titile.text = title;
    
    [_Navview.BackBnt addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    return _Navview;
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}


/**
 创建加载视图HUD
 */
-(void)creatHuD:(NSString*)title
{
    [self.HUD removeFromSuperview];
    // 隐藏时候从父控件中移除
    self.HUD.removeFromSuperViewOnHide = YES;

    self.HUD=[[MBProgressHUD alloc] initWithView:self.view];
    
    self.HUD.color = [UIColor whiteColor];//菊花背景颜色
    self.HUD.activityIndicatorColor = [UIColor lightGrayColor];//菊花颜色
    
    [self.view addSubview:self.HUD];
    
    self.HUD.labelText=title;
    self.HUD.delegate=self;
    [self.HUD show:YES];
    [self.view sendSubviewToBack:self.HUD];
}

-(UIImage *)ReplaceImage{

    //替代图片
    UIImageView *placeImg = [[UIImageView alloc]init];
    [placeImg sd_setImageWithURL:[NSURL URLWithString:@"http://www.tudou.com/programs/view/html5embed.action?type=0&code=ToaPlGMNuFg&resourceId=0_06_05_99"]];
    return placeImg.image;
}


-(RootTableview *)CreatTableview:(RootTableview *)tableview WhithRect:(CGRect)rect AndStyle:(UITableViewStyle)style{

    tableview = [[RootTableview alloc]initWithFrame:rect style:style];
    [self.view addSubview:tableview];
    return tableview;
}



-(UIView *)LinViewWhithRcet:(CGRect)rect{
    
    UIView *linview = [[UIView alloc]initWithFrame:rect];
    linview.backgroundColor = [UIColor grayColor];
    linview.alpha = 0.5;
    [self.view addSubview:linview];
    return linview;
}

-(UIButton *)CreatBntWhithRect:(CGRect)rect AndType:(UIButtonType)type AndBntImgName:(NSString *)string AndTitle:(NSString *)title
{
    UIButton *bnt=[UIButton buttonWithType:type];
    bnt.frame = rect;
    [bnt setTitle:title forState:UIControlStateNormal];
    [bnt setImage:[UIImage imageNamed:string] forState:UIControlStateNormal];
    bnt.titleLabel.font = font(13);
    [self.view addSubview:bnt];
    return bnt;
}

-(UIButton *)CreatBntAddView:(UIView *)view WhithRect:(CGRect)rect AndType:(UIButtonType)type AndBntImgName:(NSString *)string AndTitle:(NSString *)title Font:(CGFloat)font
{
    UIButton *bnt=[UIButton buttonWithType:type];
    bnt.frame = rect;
    [bnt setTitle:title forState:UIControlStateNormal];
    
    [bnt setImage:[UIImage imageNamed:string] forState:UIControlStateNormal];
    bnt.titleLabel.font = font(font);
    [view addSubview:bnt];
    return bnt;
}

-(UILabel *)CrectLabelWhithRect:(CGRect)rect{
    
    UILabel *label = [[UILabel alloc]initWithFrame:rect];
    label.font = font(16);
    label.tintColor = [UIColor grayColor];
    label.alpha = 0.6;
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    return label;
    
}

-(UIView *)Linview:(CGRect)rect BackgroundColor:(UIColor *)color AddView:(UIView *)Addview{
    UIView *view = [[UIView alloc]initWithFrame:rect];
    view.backgroundColor = color;
    [Addview addSubview:view];
    return view;
}

//计算字体高度
-(CGRect )CountTextWhithSize:(CGSize)size AndString:(NSString *)str AndfFontSize:(CGFloat)X{
    
    CGRect Myrect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Kohinoor Telugu" size:X], NSFontAttributeName,nil] context:nil];
    return Myrect;
}

-(UILabel *)CreatLabelWhithRect:(CGRect )rect Text:(NSString *)text TextColor:(UIColor *)color Font:(CGFloat)font AddView:(UIView *)fatherView {
    UILabel *label = [[UILabel alloc]initWithFrame:rect];
    label.textColor = color;
    label.font = font(SCREEN_WIDTH*font);
    label.text = text;
    [fatherView addSubview:label];
    return label;
}

-(UIImageView *)CreactImageViewWhithRect:(CGRect )rect ImageName:(NSString *)image FatherView:(UIView *)fatherview{
    
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:image]];
    imageview.frame = rect;
    [fatherview addSubview:imageview];
    return imageview;
}

- (UIAlertController *)ShowAlertWhthTitle:(NSString *)title Message:(NSString *)message CancelButtonTitle:(NSString *)cancelButtonTitle{
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    
    [alertController addAction:cancel];
    
    [self presentViewController:alertController animated:YES completion:nil];
    return alertController;
}


-(UITextField *)CrectTextFieldWhithRect:(CGRect )rect Placeholder:(NSString *)string FatherView:(UIView *)fatherView{
    
    UITextField *textfiled = [[UITextField alloc]initWithFrame:rect];
    textfiled.placeholder = string;
    textfiled.clearButtonMode = UITextFieldViewModeAlways;
    textfiled.font = font(SCREEN_WIDTH*0.05);
    [fatherView addSubview:textfiled];
    
    return textfiled;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [[SDImageCache sharedImageCache] clearMemory];
    
}

-(void)didReceiveMemoryWarning{
    
    [super didReceiveMemoryWarning];//即使没有显示在window上，也不会自动的将self.view释放。
    
    if ([self.view window] == nil) {
        
        self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
    }
}

@end